package com.heima.controller;

import com.heima.common.PageResult;
import com.heima.pojo.dto.PageSku;
import com.heima.pojo.Sku;
import com.heima.pojo.dto.SkuDto;
import com.heima.pojo.vo.BusinessTop10Vo;
import com.heima.service.SkuService;
import com.heima.utils.AliOssUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

@RestController
@RequestMapping("/api/vm-service/sku")
@Slf4j
public class SkuController {

    @Autowired
    private AliOssUtil aliOssUtil;

    @Autowired
    private SkuService skuService;

    @PostMapping("/fileUpload")
    public String uploadFile(@RequestParam("fileName") MultipartFile file) throws Exception {
        log.info("图片上传");
        log.info("文件大小: " + file.getSize());
        return aliOssUtil.upload(file.getBytes(), file.getOriginalFilename());
    }

    /**
     * 新增
     */
    @PostMapping
    public Boolean saveSku(@RequestBody SkuDto skuDto) {
        log.info("skuDto:{}", skuDto);
        Boolean isTrue = skuService.saveSku(skuDto);
        return isTrue;
    }

    /**
     * 搜索
     */
    @GetMapping("/search")
    public PageResult<Sku> searchSku(PageSku pageSku) {
        PageResult<Sku> search = skuService.search(pageSku);
        return search;
    }

    /**
     * 修改商品
     */
    @PutMapping("/{skuId}")
    public Boolean updateSku(@PathVariable Long skuId, @RequestBody SkuDto skuDto) {
        skuDto.setSkuId(skuId); // 补充 skuId
        return skuService.updateSku(skuDto);
    }

    /**
     * 获取商圈下销量排行前10的商品
     * @param businessId
     * @return
     */
    @GetMapping("/businessTop10/{businessId}")
    public List<BusinessTop10Vo> businessTop10(@PathVariable Integer businessId) {
        return skuService.businessTop10(businessId);
    }

}
